package com.sduept.nwld.secondaryequipment.evaluation.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.sduept.bigdata.fault.protection.model.ProtectionActionInfoDTO;
import com.sduept.bigdata.fault.protection.service.ProtectionActionQueryManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectResponseTimeStatistics;
import com.sduept.utils.DateUtil;


/**
 * 保护响应特性分析Rest服务
 * @author
 * @date 2017-07-05
 *
 */
@RestController
@RequestMapping("/protectAction/")
public class ProtectResponseServices {

	@Autowired
	private ProtectionActionQueryManager pa;
	@Autowired
	private SuperDAO dao;
	/**
	 * 查询一个变电站下的保护动作时间
	 * @param stationId
	 * @return
	 */

	@GetMapping("/getProtectActionByStationId/{stationId}")
	public String getProtectActionByStationId(@PathVariable("stationId") String stationId){
		
		//String result = protectActionCache.getProtectActionByStationId(stationId);
		return null;
	}
	
	/**
	 * 查询全部的保护动作时间（主菜单保护响应时间分析模块）
	 * @return[[name,faultTime,actTime]]
	 */

	@GetMapping("/getProtectActionAll")
	public String getProtectActionAll1(){
		JSONArray jsa = new JSONArray();
//		List<ProtectResponseTimeStatistics> prts = dao.findAll(ProtectResponseTimeStatistics.class);
		List<ProtectResponseTimeStatistics> prts = dao.findByCondition(ProtectResponseTimeStatistics.class,"month is not null");
		for (ProtectResponseTimeStatistics prt : prts) {
			JSONArray jsArr = new JSONArray();
			jsArr.add(prt.getArea());
			jsArr.add(prt.getYear());
			jsArr.add(prt.getMonth());
			jsArr.add(prt.getAvgActTime());
			jsArr.add(prt.getMaxActTime());
			jsa.add(jsArr);
		}
		return jsa.toString();
	}
	/**
	 * 查询年度保护动作时间（主菜单保护响应时间分析模块，月为null的情况）
	 * @return[[name,faultTime,actTime]]
	 */

	@GetMapping("/getProtectActionYearData")
	public String getProtectActionYearData(){
		JSONArray jsa = new JSONArray();
		List<ProtectResponseTimeStatistics> prts = dao.findByCondition(ProtectResponseTimeStatistics.class,"month is null");
		for (ProtectResponseTimeStatistics prt : prts) {
			JSONArray jsArr = new JSONArray();
			jsArr.add(prt.getArea());
			jsArr.add(prt.getYear());
			jsArr.add(prt.getMonth());
			jsArr.add(prt.getAvgActTime());
			jsArr.add(prt.getMaxActTime());
			jsa.add(jsArr);
		}
		return jsa.toString();
	}
	
	/**
	 * 获取变电站保护动作时间
	 * 
	 * @return
	 */

	@GetMapping("/getProtectActTimesByStationId/{sid}/{startTime}/{endTime}")
	public String getProtectActTimesByStationId(@PathVariable("sid") String sid,@PathVariable("startTime") String startTime,@PathVariable("endTime") String endTime) {
		String[] station_id={sid};
		Date startDate=DateUtil.getDateByString(startTime, DateUtil.pattern_ymd);
		Date endDate=DateUtil.getDateByString(endTime, DateUtil.pattern_ymd);
		List<ProtectionActionInfoDTO> paDtoList=pa.getProtectionActionTimeDtoByStationId(station_id,startDate,endDate);
		List<ProtectionActionInfoDTO> rtn=new ArrayList<>();
		for(ProtectionActionInfoDTO p:paDtoList){
			if("发跳".equals(p.getActionType())&&p.getDistance0MomentsTime()>0){
				rtn.add(p);
			}
		}
		return net.sf.json.JSONArray.fromObject(rtn).toString();
		 
	}
	
}
